﻿Imports MySql.Data.MySqlClient

Public Class login
    Private connStr As String = "Database=zoo;" & _
                    "Data Source=localhost;" & _
                    "User Id=root;Password=root;" & _
                    "Connection Timeout=20"
    Private Sub login_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
    End Sub
    Public Sub login()
        Try
            Dim query As String = "SELECT * FROM usuario"
            Dim connection As New MySqlConnection(connStr)
            Dim cmd As New MySqlCommand(query, connection)
            Dim loginok As Boolean
            connection.Open()

            Dim reader As MySqlDataReader
            reader = cmd.ExecuteReader()

            While reader.Read()
                If txt_usuario.Text = reader.GetString(1) And txt_password.Text = reader.GetString(2) Then
                    MsgBox("Acceso correcto")
                    loginok = True

                ElseIf txt_usuario.Text = reader.GetString(1) And Not txt_password.Text = reader.GetString(2) And loginok = False Then
                    'MsgBox("Contraseña incorrecta")


                ElseIf Not txt_usuario.Text = reader.GetString(1) And loginok = False Then
                    ' MsgBox("Usuario incorrecto")

                End If

            End While
            If loginok = True Then
                Me.Hide()
                Form1.Show()
            Else
                MsgBox("Login incorrecto")
            End If
            reader.Close()
            connection.Close()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

    Private Sub btn_acceso_Click(sender As System.Object, e As System.EventArgs) Handles btn_acceso.Click
        login()
    End Sub
End Class